// ContextObjectMenu.cpp
//
#include "ContextObjectMenu.h"
#include "MemCheck.h"

using namespace IDS::IDEAS::OpenGLView::GUIElements;

// ------------------------------------------------------------------------------------------------
ContextObjectMenu::ContextObjectMenu(QGraphicsScene *scene, QList<QAction *> &actions, QMenu *parent )
: QMenu(parent),
  gs(scene)
{
	setWindowOpacity(0.90);

	addActions(actions);

	cp = new CustomProxy(0,Qt::Widget);
	cp->setWidget((QMenu*)this);
}

// ------------------------------------------------------------------------------------------------
ContextObjectMenu::ContextObjectMenu(QGraphicsScene *scene, QMenu *parent)
: QMenu(parent),
  gs(scene)
{
	setWindowOpacity(0.90);

	cp = new CustomProxy(0,Qt::Widget);
	cp->setWidget((QMenu*)this);
}

// ------------------------------------------------------------------------------------------------
ContextObjectMenu::~ContextObjectMenu()
{
	// remove the custom proxy
	if(cp)
	{
		for(size_t i=0; i<children.size(); ++i)
		{
		//	gs->removeItem(children[i]);
			children[i]->setWidget(NULL);
			delete children[i];
		}
		children.clear();

		//gs->removeItem(cp);
		cp->setWidget(NULL);

		//delete cp;
		cp = NULL;
	}
}

// ------------------------------------------------------------------------------------------------
QMenu *ContextObjectMenu::addMenu( const QString &title )
{
	QMenu *ret = QMenu::addMenu(title);

	QGraphicsProxyWidget *pw = cp->createProxyForChildWidget(ret);

	if(pw)
	{
		children.push_back(pw);
		ret->setWindowOpacity(0.90);
		pw->setZValue(1e30);
		//gs->addItem(pw);
	}

	return ret;
}

// ------------------------------------------------------------------------------------------------
void ContextObjectMenu::popUp(int xPos,int yPos)
{
	cp->setPos(xPos,yPos);
	cp->show();
	gs->addItem(cp);
}